<?php

/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 


/**
 *  @file
 *  @Author Samuel Maura
 *  Wiki page content 
 *
 */

require_once("".$_SERVER['DOCUMENT_ROOT']."/xenix/models/dbQuery.inc");


class Entries extends DBQuery {

  private $id;
  private $title;
  private $content;
  private $user_id;
  private $ip;
  private $revision;
  private $accessed;
  private $group_id;
  private $restriction_id;
  
  public function __construct(){
    $this->connection = dbManager::connect();  
  }

// Create new page
function add($row_data){
  $this->page_table = array();
  $this->page_table['id'] = $row_data['id'];
  $this->page_table['title'] = $row_data['title'];
  $this->page_table['content'] = $row_data['content'];
  $this->page_table['user_id'] = $row_data['user_id'];
  $this->page_table['group_id'] = $row_data['group_id'];
  $this->page_table['restriction_id'] = $row_data['restriction_id'];
  $this->page_table['ip'] = $row_data['ip'];
  
  $query = "INSERT INTO entries (id,title,content,user_id,group_id,restriction_id,author_ip,revision) values 
    (".$this->page_table['id'].",'".$this->page_table['title']."','".$this->page_table['content']."',
    '".$this->page_table['user_id']."','".$this->page_table['group_id']."', '".$this->page_table['restriction_id']."','".$this->page_table['ip']."',1)";
   $this->queryString($query);
  }


 function updateEntry($page_id,$row_data){
 

 
 // update entry table
  $this->page_table = array();
  $this->page_table['id'] = $page_id;
  $this->page_table['revision'] = $row_data['revision'];
  $this->page_table['last_modified'] = $row_data['last_modified'];
  $this->page_table['title'] = $row_data['title'];
  $this->page_table['content'] = $row_data['content'];
  $this->page_table['user_id'] = $row_data['user_id'];
  $this->page_table['group_id'] = $row_data['group_id'];
  $this->page_table['restriction_id'] = $row_data['restriction_id'];
  $this->page_table['ip'] = $row_data['ip'];


 // add row to revision table
 $create_rev = "insert into entries_revisions (id,title,content,user_id,author_ip,revision,created) 
 select id,title,content,user_id,author_ip,revision,'".$this->page_table['last_modified']."' from entries where id ='".$page_id."'";
 $this->queryString($create_rev);
  
  $query = "UPDATE  entries set title='".$this->page_table['title']."',
  content='".$this->page_table['content']."',user_id='".$this->page_table['user_id']."',
  group_id='".$this->page_table['group_id']."', restriction_id='".$this->page_table['restriction_id']."',
  author_ip='".$this->page_table['ip']."',revision='".$this->page_table['revision']."',
  last_modified='".$this->page_table['last_modified']."' where id='".$this->page_table['id']."'";
  $this->queryString($query);  
 } 
 
 
function getPageHistory($page_id){
   $query ="select id,title,content,created,user_id,author_ip,revision from entries where id=".$page_id." 
   union all select id,title,content,created,user_id,author_ip,revision from entries_revisions where id =".$page_id."";
  $this->queryString($query);
  $result = $this->rowCount() > 0 ? $this->getRowSet() : 0;
  return $result;     
}

function getAllPages(){
  $query ="SELECT * FROM entries where status='active' limit 50";
  $this->queryString($query);
  $result = $this->rowCount() > 0 ? $this->getRowSet() : 0;
  return $result; 
}

function getPage($page_id){
  $query ="SELECT * FROM entries where id = ".$page_id." ";
  $this->queryString($query);
  $result = $this->rowCount() > 0 ? $this->getRowSet() : 0;
  return $result; 
}

function deletePage($page_id){
   $query ="UPDATE entries set status='deleted' where id = ".$page_id." ";
  $this->queryString($query);
}

function displayTitle($title){
  return join(" ", explode("_",$title)); 
}

 function titleExist($title){
   $query = "SELECT * from entries where title ='".$title."' ";
   $this->queryString($query);
   if ($this->rowCount() != 0){
     return true;
   }
   else {
     return  false;
   }
 }


}

?>